/******************************************************************************************************
Copyright (C) 2008 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/

function nmrProcessingTest()
{	
	var spec = new NMRSpectrum( nmr.activeSpectrum() );
	var p = new NMRProcessing(spec.proc);
	print(p.getParameter("DC"));
	print(p.getParameter("FT"));
	print(p.getParameter("FT[1].Quadrature"));
	print(p.getParameter("zf"));
	print(p.getParameter("LP"));
	print(p.getParameter("LP.Method") );
	print(p.getParameter("BC") );
	print(p.getParameter("BC.apply") );
	print(p.getParameter("BC.algorithm") );
	print(p.getParameter("BC.filter") );
	print(p.getParameter("ds"));
	print(p.getParameter("ds.method"));
	print(p.getParameter("ss"));
	print(p.getParameter("ss.signals"));
	print(p.getParameter("smooth"));
	print(p.getParameter("smooth.method"));
	print(p.getParameter("smooth.average.span"));
	print(p.getParameter("pp"));
	print(p.getParameter("pp.apply"));
	print(p.getParameter("pp.peaktype"));
	print(p.getParameter("binning"));
	print(p.getParameter("binning.width"));
	print(p.getParameter("ref"));
	print(p.getParameter("ref.autotune"));
	print(p.getParameter("rev[1]"));
	print(p.getParameter("norm"));
	print(p.getParameter("norm.range"));
	print(p.getParameter("trunc"));
	print(p.getParameter("trunc.after"));
	print(p.getParameter("comp"));
	print(p.getParameter("comp.ratio"));
	print(p.getParameter("mult"));
	spec.proc = p;
	spec.process();
	//var result = spec.process();
	//MessageBox.information("Process "+result); 
	mainWindow.activeWindow().update();
}

function integralsTest()
{
	var spec = new NMRSpectrum( nmr.activeSpectrum() );
	var p = new NMRProcessing(spec.proc);
	//1D Automatic integration parameters
	//p.setParameter("integration.method", "Auto");
	//Derivative Method
	//p.setParameter("integration.auto.algorithm", "Derivative");
	//p.setParameter("integration.auto.sensitivity", 7);
	//p.setParameter("integration.auto.autosensitivity", true);
	//PeakPicking Method
	//p.setParameter("integration.auto.algorithm", "PeakPicking");
	//p.setParameter("integration.auto.jmax", 25);
	//p.setParameter("integration.auto.peakfactor", 6);

	//integral list import 
	//var regs = SpectrumRegion.importList("/home/isaac/tmp/mnova/myintegrals.txt");
	var regs = new Array(2);
	regs[0]  = new SpectrumRegion(2.0, 2.5);
	regs[1] = new SpectrumRegion(7.0, 7.5);
	p.setParameter("integration.method", "Predefined");
	p.setParameter("integration.regions", regs);
	p.setParameter("integration.apply", true);
	//integral list export:
	//SpectrumRegion.exportList(regs, "/home/isaac/tmp/mnova/exported_integrals.txt"); 
	spec.proc = p;
	spec.process();
	mainWindow.activeWindow().update();
}

function cutsTest()
{
	var spec = new NMRSpectrum( nmr.activeSpectrum() );
	var p = new NMRProcessing(spec.proc);
	var regs = spec.autoDetectNoiseRegions(); //this returns a list of SpectrumRegion object
	regs.push(new SpectrumRegion(0.5, -0.5)); //We can add/remove regions from the list
	//regs.push(new SpectrumRegion(0.75, 0.9));
	print(regs);
	p.setParameter("cuts.apply", true);
	p.setParameter("cuts.list", regs); //now we apply the cuts list to the process object
	spec.proc = p;
	spec.process();
	//var result = spec.process();
	//MessageBox.information("Process "+result); 
	mainWindow.activeWindow().update();
}

function apodizationTest()
{
	var spec = new NMRSpectrum( nmr.activeSpectrum() );
	var p = new NMRProcessing(spec.proc);
	//Along dim 1
	print(p.getParameter("Apodization[1]"));
	p.setParameter("Apodization[1].Exp.apply", true);
	p.setParameter("Apodization[1].Exp.value", 0.5);
	p.setParameter("Apodization[1].fp.apply", true);
	p.setParameter("Apodization[1].fp.value", 0.5);
	print(p.getParameter("Apodization[1]"));
	spec.proc = p;
	spec.process();
	mainWindow.activeWindow().update();
}

function phaseCorrectionTest()
{
        var spec = new NMRSpectrum( nmr.activeSpectrum() );
        var p = new NMRProcessing(spec.proc);
        print("Old Method: ", p.getParameter("PC.algorithms"));
        p.setParameter("PC.method", "Global, Metabonomics");
        print("New Method: ", p.getParameter("PC.algorithms"));
//      p.setParameter("PC.method", "Manual");
//      p.setParameter("PC.PH0", 0);
//      p.setParameter("PC.PH1", Math.PI / 2);
	   print("Init: {0}".format(p.getParameter("PC.InitMode")));
	   p.setParameter("PC.InitMode", "InitZero");
   	   print("New Init: {0}".format(p.getParameter("PC.InitMode")));
        spec.proc = p;
        spec.process();
        mainWindow.activeWindow().update();
}

function referenceTest()
{
	var spec = new NMRSpectrum(nmr.activeSpectrum());
	spec.reference(1,6.0,3.0,true,0.1, "Script Annotation"); //auto tune tries to find the maximum around 5.9 with a tolerance of 0.1	
	spec.update();
	mainWindow.activeWindow().update();
	
/*	
	var spec = new NMRSpectrum( nmr.activeSpectrum() );
	var p = new NMRProcessing(spec.proc);	

	p.setParameter("ref[1].apply", true);
	p.setParameter("ref[1].shift", 4.45, 4.40);
	p.setParameter("ref[1].annotation", "Script Annotation");
	spec.proc = p;
	spec.process();	
	mainWindow.activeWindow().update();
*/
}

function multipointBaselineCorrectionTest()
{
	var spec = new NMRSpectrum(nmr.activeSpectrum());
	var p = new NMRProcessing(spec.proc);
//original params
	print(p.getParameter("BC[1].BCMultipoint"));
//set new params
	p.setParameter("BC[1].BCMultipoint.Apply", true);
	p.setParameter("BC[1].BCMultipoint.smooth", 5);
	p.setParameter("BC[1].BCMultipoint.Algorithm.type", "Segments");
	p.setParameter("BC[1].bcmultipoint.algorithm.lambda", 20);
//set point params. An array of objects with x and y properties is needed. If the y property is not set
//the value of the spectrum height will be use by default otherwise the point will be marked as "free"
	var newPoints = []
	var point = {x:1.0} //x coordinates in PPM. y propery not set, the spectrum height in that point will be used
	newPoints.push(point);
	var anotherPoint = {x:8.0, y:5}
	newPoints.push(anotherPoint);
	p.setParameter("bc[1].bcmultipoint.points", newPoints);
//check the modified values
	print(p.getParameter("BC[1].BCMultipoint"));
//process spectrum
	spec.process(p);
//update
	spec.update();
	mainWindow.activeWindow().update();
}

function normalizeTest() {
	var spec, p;
	spec = new NMRSpectrum(nmr.activeSpectrum());
	p = new NMRProcessing(spec.proc);
	print(p.getParameter("norm"));
	print(p.getParameter("norm.range"));
	p.setParameter("norm.method", "LargestPeakRange");
	p.setParameter("norm.range", [5.0, 5.5]);
	p.setParameter("norm.apply", true);
	p.setParameter("norm.value", 1);
	print(p.getParameter("norm"));
	print(p.getParameter("norm.range"));
	spec.process(p);
	spec.update();
	mainWindow.activeWindow().update();
}

function freqShiftTest() {
	'use strict'
	
	var spec = new NMRSpectrum(nmr.activeSpectrum()),
		p = new NMRProcessing(spec.proc),
		freqShift = p.getParameter("FreqShift");
	
	print(freqShift);
}

function printParameter(aParam) {
	'use strict'
	
	var spec = new NMRSpectrum(nmr.activeSpectrum()),
		p = new NMRProcessing(spec.proc),
		paramValue = p.getParameter(aParam);
	
	print(aParam + ": " + JSON.stringify(paramValue, null, " "));	
}

function setParameter(aParam, aValue) {
	'use strict'
	
	var spec = new NMRSpectrum(nmr.activeSpectrum()),
		p = new NMRProcessing(spec.proc),
		paramValue = p.getParameter(aParam);
	
	print(aParam + ": " + JSON.stringify(paramValue, null, " "));		
	p.setParameter(aParam, aValue);
	paramValue = p.getParameter(aParam);
	print(aParam + ": " + JSON.stringify(paramValue, null, " "));
}	
